<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/module.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../high_level_components.html" title="Chapter 2. High Level Components">
<link rel="prev" href="boost_python_iterator_hpp.html" title="boost/python/iterator.hpp">
<link rel="next" href="boost_python_operators_hpp.html" title="boost/python/operators.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_iterator_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_operators_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="high_level_components.boost_python_module_hpp"></a><a class="link" href="boost_python_module_hpp.html" title="boost/python/module.hpp">boost/python/module.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.macros">Macros</a></span></dt>
<dt><span class="section"><a href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.examples">Examples</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_module_hpp.introduction"></a><a class="link" href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          This header provides the basic facilities needed to create a Boost.Python
          extension module.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_module_hpp.macros"></a><a class="link" href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.macros" title="Macros">Macros</a>
</h3></div></div></div>
<p>
          <code class="computeroutput"><span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">name</span><span class="special">)</span></code>
          is used to declare Python <a href="http://www.python.org/doc/2.2/ext/methodTable.html#SECTION003400000000000000000" target="_top">module
          initialization functions</a>. The name argument must exactly match
          the name of the module to be initialized, and must conform to Python's
          <a href="http://www.python.org/doc/2.2/ref/identifiers.html" target="_top">identifier
          naming rules</a>. Where you would normally write
        </p>
<pre class="programlisting"><span class="keyword">extern</span> <span class="string">"C"</span> <span class="keyword">void</span> <span class="identifier">initname</span><span class="special">()</span>
<span class="special">{</span>
   <span class="special">...</span>
<span class="special">}</span>
</pre>
<p>
          Boost.Python modules should be initialized with
        </p>
<pre class="programlisting"><span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">name</span><span class="special">)</span>
<span class="special">{</span>
   <span class="special">...</span>
<span class="special">}</span>
</pre>
<p>
          This macro generates two functions in the scope where it is used: <code class="computeroutput"><span class="keyword">extern</span> <span class="string">"C"</span>
          <span class="keyword">void</span> <span class="identifier">initname</span><span class="special">()</span></code>, and <code class="computeroutput"><span class="keyword">void</span>
          <span class="identifier">init_module_name</span><span class="special">()</span></code>,
          whose body must follow the macro invocation. <code class="computeroutput"><span class="identifier">init_name</span></code>
          passes <code class="computeroutput"><span class="identifier">init_module_name</span></code>
          to <a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.functions" title="Functions">handle_exception()</a>
          so that any C++ exceptions generated are safely processeed. During the
          body of <code class="computeroutput"><span class="identifier">init_name</span></code>, the
          <a class="link" href="boost_python_scope_hpp.html" title="boost/python/scope.hpp">current scope</a>
          refers to the module being initialized.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_module_hpp.examples"></a><a class="link" href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.examples" title="Examples">Examples</a>
</h3></div></div></div>
<p>
          C++ module definition:
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">xxx</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">throw</span> <span class="string">"something bad happened"</span>
<span class="special">}</span>
</pre>
<p>
          Interactive Python:
        </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">import</span> <span class="identifier">xxx</span>
<span class="identifier">Traceback</span> <span class="special">(</span><span class="identifier">most</span> <span class="identifier">recent</span> <span class="identifier">call</span> <span class="identifier">last</span><span class="special">):</span>
  <span class="identifier">File</span> <span class="string">""</span><span class="special">,</span> <span class="identifier">line</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">in</span> <span class="special">?</span>
<span class="identifier">RuntimeError</span><span class="special">:</span> <span class="identifier">Unidentifiable</span> <span class="identifier">C</span><span class="special">++</span> <span class="identifier">Exception</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_iterator_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_operators_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
